from tkinter import*

from tkinter import filedialog,messagebox

from tkinter.ttk import Scrollbar,Checkbutton,Label,Button

import os

import sys

class Notepad(Tk):

    def __init__(self):

        super().__init__()
        self.file_name = None

        self.set_window()

        self.create_menu_bar()

        self.create_tool_bar()

        self.create_body()
        self.create_pop_menu()



    def set_window(self):

        self.title("待办事项")

        max_width,max_height=self.maxsize()

        aligh_center="800x600+%d+%d"%((max_width-800)/2,(max_height-600)/2)

        self.geometry(aligh_center)

        self.iconbitmap()

    def create_menu_bar(self):

        menu_bar = Menu(self)



        file_menu=Menu(menu_bar,tearoff=0)

        file_menu.add_command(label='新建', accelerator="Ctrl+N",command=self.new_file)

        file_menu.add_command(label='打开', accelerator="Ctrl+O", command=self.open_file)
        file_menu.add_command(label='保存', accelerator="Ctrl+S", command=self.save_file)
        file_menu.add_command(label='另存为', accelerator="Ctrl+B", command=self.save_as)

        file_menu.add_separator()

        file_menu.add_command(label='退出', accelerator="Ctrl+A", command=self.exit_notepad)



        menu_bar.add_cascade(label='文件',menu=file_menu)





        editor_menu = Menu(menu_bar, tearoff=0)

        editor_menu.add_command(label='撤销', accelerator="Ctrl+2", command='')

        editor_menu.add_separator()

        editor_menu.add_command(label='剪切', accelerator="Ctrl+4", command='')

        editor_menu.add_command(label='复制', accelerator="Ctrl+5", command='')

        editor_menu.add_command(label='粘贴', accelerator="Ctrl+6", command='')

        editor_menu.add_command(label='查找', accelerator="Ctrl+7", command='')

        editor_menu.add_separator()

        editor_menu.add_command(label='全选', accelerator="Ctrl+8", command='')



        menu_bar.add_cascade(label='编辑', menu=editor_menu)



        view_menu = Menu(menu_bar, tearoff=0)

        self.is_show_line_num=IntVar()

        self.is_show_line_num.set(1)

        view_menu.add_checkbutton(label="显示行号",

                                  onvalue=0,offvalue=1,

                                  variable=self.is_show_line_num,command="")

        self.is_heighline_line=IntVar()

        view_menu.add_checkbutton(label="亮度当前行",variable=self.is_show_line_num,command="")

        view_menu.add_separator()

        themes_menu=Menu(menu_bar,tearoff=0)

        themes_menu.add_cascade(label="主题1",command="")

        themes_menu.add_cascade(label="主题2",command="")

        view_menu.add_cascade(label="主题",menu=themes_menu)

        menu_bar.add_cascade(label='视图', menu=view_menu)

        about_menu = Menu(menu_bar, tearoff=0)

        about_menu.add_command(label="关于",command="")

        about_menu.add_command(label="帮助",command="")

        menu_bar.add_cascade(label='关于', menu=about_menu)





        self['menu']=menu_bar

    def create_tool_bar(self):

        tool_bar=Frame(self,height=25,background="#FFFFFF")

        tool_bar.pack(fill="x")

    def create_body(self):

        self.line_number_bar=Text(self,width=4,padx=3,takefocus=0,border=0,background="#f0E68C",state="disable")
        self.line_number_bar.pack(side='left',fill='y')
        self.context_text=Text(self,wrap="word",undo=True)
        self.context_text.bind("<Control-o>", self.open_file)
        self.context_text.bind("<Control-O>", self.open_file)
        self.context_text.bind("<Control-s>", self.save_file)
        self.context_text.bind("<Control-S>", self.save_file)
        self.context_text.bind("<Control-c>", self.new_file)
        self.context_text.bind("<Control-C>", self.new_file)
        self.context_text.pack(fill='both',expand="yes")

        self.context_text.tag_config("active_line",background="#EEEEE0")

        scroll_bar=Scrollbar(self.context_text)

        scroll_bar['command']=self.context_text.yview

        self.context_text["yscrollcommand"]=scroll_bar.set

        scroll_bar.pack(side="right",fill="y")

    def open_file(self,event=None):

        input_file=filedialog.askopenfilename(filetypes=[("所有文件","*.*"),("文件文档","*.txt")])
        if input_file:
            self.title("{}***789".format(os.path.basename(input_file)))
            self.file_name=input_file
            with open(input_file,'r')as _file:
                self.context_text.insert(1.0,_file.read())
    def write_to_file(self,file_name):
        try:
            content=self.context_text.get(1.0,END)
            with open(file_name,'w')as _file:
                _file.write(content)
                self.title("{}---789".format(os.path.basename(file_name)))
        except IOError:
            messagebox.showerror("错误","文件保存失败！")
    def save_file(self,event=None):
        if not self.file_name:
            self.save_as()
        else:
            self.write_to_file(self.file_name)
        self.write_to_file(self.file_name)
    def new_file(self,event=None):
        self.title("新建---Notepad")
        self.context_text.delete(1.0,END)
        self.file_name=None
    def save_as(self,event=None):
        input_file = filedialog.askopenfilename(filetypes=[("所有文件", "*.*"),
                                                           ("文件文档", "*.txt")])
        if input_file:
            self.file_name=input_file
            self.write_to_file(self.file_name)
    def exit_notepad(self):
        if messagebox.askokcancel("退出","确定退出吗？"):
            self.destroy()
    def create_pop_menu(self):
        pop_menu=Menu(self.context_text,tearoff=0)
        for item1,item2,in zip(['剪切','复制','粘贴','撤销','恢复'],
                               ['cut','copy','paste','undo','redo']):
            pop_menu.add_command(label=item1,compound='left',command=self.tool_bar_action(item2))
        pop_menu.add_separator()
        pop_menu.add_command(label="全选",command="")
        #self.context_text.bind("<Button-3",lambda event:pop_menu.tk_popup(event.x_root,event.y_root))
        return "break"
    def handle_menu_action(self,action_type):
        if action_type=="撤销":
            self.context_text.event_generate("<<Undo>>")
        elif action_type=="恢复":
            self.context_text.event_generate("<<Rndo>>")
        elif action_type == "剪切":
            self.context_text.event_generate("<<Cut>>")
        elif action_type == "复制":
            self.context_text.event_generate("<<Copy>>")
        elif action_type == "cut":
            self.context_text.event_generate("<<Paste>>")


    def tool_bar_action(self,action_type):
        def handle():
            if action_type=="open_flie":
                self.open_file()
            elif action_type == "save":
                self.save_file()
            elif action_type == "new_file":
                self.new_file()
            elif action_type=="cut":
                self.handle_menu_action("剪切")
            elif action_type == "copy":
                self.handle_menu_action("复制")
            elif action_type == "paste":
                self.handle_menu_action("粘贴")
            elif action_type=="undo":
                self.handle_menu_action("撤销")
            elif action_type=="redo":
                self.handle_menu_action("恢复")



if __name__ == '__main__':
    app = Notepad()
    app.mainloop()
